<template>
  <el-container>
    <el-header>
      <el-row class="header-row">
        流程名称:<el-input v-model="conditions[0].value" style="width:150px;" placeholder="请输入内容" clearable />
        流程编号:<el-input v-model="conditions[1].value" style="width:150px;" placeholder="请输入内容" clearable />
        节点类型:<el-select v-model="conditions[2].value" placeholder="请选择">
          <el-option
            v-for="item in dir_node_type"
            :key="item.id"
            :label="item.label"
            :value="item.data"
          />
        </el-select>
        <el-button class="table-header-btn" type="primary" icon="el-icon-search" @click="search">查询</el-button>
      </el-row>
    </el-header>
    <el-main>
      <el-col :span="1"><div class="grid-content bg-purple-light">
        <el-button class="table-header-btn" type="primary" icon="el-icon-plus" size="mini" @click="confirm">确定</el-button>
      </div></el-col>
      <template>
        <el-table :data="process" stripe border style="width: 100%">
          <el-table-column prop="procno" label="流程编号" width="180" />
          <el-table-column prop="nodesn" label="节点" width="180" />
          <el-table-column prop="nodename" label="节点名称" />
          <el-table-column
            fixed="right"
            label="操作"
            width="180"
          >
            <template slot-scope="scope">
              <transition name="slide-fade" mode="out-in">
                <el-button v-if="selected.indexOf(scope.row.id) === -1" type="primary" size="mini" @click="addSelect(scope.row)">添加</el-button>
                <el-button v-if="selected.indexOf(scope.row.id) !== -1" size="mini" @click="cancelSelect(scope.row)">取消</el-button>
              </transition>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-main>
    <el-footer>
      <div class="block">
        <el-pagination
          :current-page="page.current"
          :page-sizes="[5,10, 20, 50, 100]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-footer>
  </el-container>
</template>
<script>
import request from '@/utils/request'
export default {
  name: 'RoleList',
  props: {
    procno: {
      type: String,
      default: ''
    },
    nodeType: {
      type: String,
      default: ''
    }
  },
  data: function() {
    return {
      conditions: [
        {
          field: 'procname',
          type: 'STRING',
          value: '',
          symbol: 'LIKE'
        },
        {
          field: 'procno',
          type: 'STRING',
          value: '',
          symbol: 'EQ'
        },
        {
          field: 'node_type',
          type: 'STRING',
          value: '',
          symbol: 'EQ'
        }
      ],
      orders: [
        { field: 'nodesn' }
      ],
      selected: [],
      selectObjs: [],
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      process: []
    }
  },
  computed: {
    dir_node_type: function() {
      return this.$store.state.directory.directory['node_type']
    }
  },
  created() {
    if (this.procno) {
      this.conditions[1].value = this.procno
    }
    if (this.nodeType) {
      this.conditions[2].value = this.nodeType
    }
    this.$store.commit('directory/setDirectory', 'node_type')
    this.query()
  },
  methods: {
    // 添加到选择中
    addSelect: function(row) {
      this.selectObjs.push(row)
      this.selected.push(row.id)
    },
    // 取消选择
    cancelSelect: function(row) {
      this.selectObjs = this.selectObjs.filter(item => {
        return item.id !== row.id
      })
      this.selected = this.selected.filter(item => {
        return item !== row.id
      })
    },
    confirm: function() {
      this.$emit('confirm', this.selected, this.selectObjs)
      const that = this
      const time = setTimeout(() => {
        // 同时清空所选
        that.selected = []
        that.selectObjs = []
        clearTimeout(time)
      }, 500)
    },
    // 搜索
    search: function() {
      this.page.current = 1
      this.query()
    },
    // 查询
    query: function() {
      this.$progress.start()
      this.page.conditions = this.conditions
      this.page.orders = this.orders
      request({
        url: '/task/node/query',
        method: 'post',
        data: this.page
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.page.total = res.data.total
          this.process = res.data.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    // 每页显示数目发生变化时
    handleSizeChange: function(val) {
      this.page.size = val
      this.page.current = 1
      this.query()
    },
    // 当前页发生变化时
    handleCurrentChange: function(val) {
      this.page.current = val
      this.query()
    }
  }
}
</script>
